package com.rb.config;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Properties;

/**
 * Created by Brooks on 2014/4/10.
 */
@Configuration
//@PropertySource("/jdbc.properties")
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.rb", "com.rb.dao", "com.rb.service"})
public class DefaultConfig {
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver irv = new InternalResourceViewResolver();
        irv.setPrefix("/WEB-INF/views/");
        irv.setSuffix(".jsp");
        return irv;
    }

    //    @Value("${jdbc.driverClass}")
    String driverClass = "com.mysql.jdbc.Driver";
    //@Value("${jdbc.url}")
    String url = "jdbc:mysql://localhost:3306/dkhwechat";
    //@Value("${jdbc.user}")
    String user = "root";
    //@Value("${jdbc.password}")
    String password = "mysql5#";

    private static void SshConfig() throws JSchException {
        int lport;
        String rhost;
        int rport;

        String user = "root";
        String password = "Aliyun2014";
        String host = "115.28.224.36";
        int port = 22;

        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        lport = 9999;
        rhost = "localhost";
        rport = 3306;
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        System.out.println("Establishing Connection...");
        session.connect();
        int assinged_port = session.setPortForwardingL(lport, rhost, rport);
        System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport);
    }

    @Bean
    public DataSource dataSource() throws PropertyVetoException, JSchException {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() throws PropertyVetoException, JSchException {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setPackagesToScan("com.rb.domain");
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        hibernateProperties.setProperty("hibernate.show_sql", "true");
        sessionFactoryBean.setHibernateProperties(hibernateProperties);
        return sessionFactoryBean;
    }

    @Bean
    public HibernateTransactionManager txManager() throws PropertyVetoException, JSchException {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory().getObject());
        return txManager;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxUploadSize(5120000);
        multipartResolver.setMaxInMemorySize(512);
        return multipartResolver;
    }

    @Bean
    public StandardServletMultipartResolver portletMultipartResolver() {
        StandardServletMultipartResolver portletMultipartResolver = new StandardServletMultipartResolver();
        return portletMultipartResolver;
    }

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        return new RequestMappingHandlerMapping();
    }

    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();

        ArrayList<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

        ArrayList<MediaType> typeList = new ArrayList<MediaType>();
        MediaType mediaType = MediaType.parseMediaType("text/html;charset=UTF-8");
        typeList.add(mediaType);
        converter.setSupportedMediaTypes(typeList);

        list.add(converter);
        adapter.setMessageConverters(list);
        return adapter;
    }
    /* @Bean
    public DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean(){
        DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();
        Resource res = new ClassPathResource("/com/rb/mapper/dozerBeanMapping.xml");
        dozerBeanMapperFactoryBean.setMappingFiles(new Resource[]{res});
        return dozerBeanMapperFactoryBean;
    }*/
}